package com.elive.controller.backend;

import com.elive.common.Const;
import com.elive.common.ResponseCode;
import com.elive.common.ServerResponse;
import com.elive.pojo.Label;
import com.elive.pojo.User;
import com.elive.service.IUserService;
import com.elive.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by wuyin on 2018/3/6.
 */
@Controller
@RequestMapping("/host/user")
public class UserManagerController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping(value="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){

        ServerResponse<User> response = iUserService.login(username,password);
        if(response.isSuccess()){
            User user = response.getData();
            if(user.getRole() == Const.Role.ROLE_HOST){
                session.setAttribute(Const.CURRENT_USER,user);
                return response;
            }else {
                return ServerResponse.createByErrorMessage("不是房主无法登录");
            }

        }
        return response;
    }

    @RequestMapping(value="logout.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session) {
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }


    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user){
        return iUserService.register(user);
    }

    @RequestMapping(value = "updateUser.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateUser(User user, String labels, HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        // 如果未登录
        if(currentUser == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }else{
            //System.out.println( "__________"+currentUser.getId());
            user.setId(currentUser.getId());
            //传入 user label
            System.out.println(user.getPhone());
            System.out.println(labels);
            //进行更新处理
            return iUserService.updateUser(user,labels);

        }

    }

    @RequestMapping(value = "getUserInfo.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getUserInfo(HttpSession httpSession){
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }else {
            return iUserService.getUserInfo(currentUser.getId());
        }
    }

    @RequestMapping(value = "resetPassword.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(String oldPassword, String newPassword, HttpSession httpSession){
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null) {
            return  ServerResponse.createByErrorMessage("用户未登录");
        }else {
            return iUserService.resetPassword(oldPassword,newPassword,currentUser);
        }

    }

    @RequestMapping(value = "aa.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> aa(){
        return ServerResponse.createBySuccessMessage("login.");
    }
}
